package com.cndw.kungfu.model.pvp.api;

import java.util.Collection;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.controller.CrossController;
import com.cndw.kungfu.ext.Aes;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.activity.domain.CrossServer;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.net.SocketClient;
import com.cndw.rpg.net.SocketRequest;

/**
 * 跨服api的基类.其他接口在此扩展
 * @author fantadust
 *
 */
class ApiClient {
	public static final String CrossController = CrossController.class.getSimpleName();
	
	static {
		load();
	}
	protected synchronized static void load(){
		Collection<CrossServer> allServers = CrossServer.getAll();
		for (CrossServer e : allServers) {
			SocketClient.addClient(e.getServerId(), e.getSvrId(), e.getIp(), e.getPort());
		}
	}
	
	protected static Object setdApi(int serverId, String method, Object... parms) {
		return sendApiM(serverId, method, 0, parms);
	}
	
	protected static Object sendApiByTimeOut(int serverId, String method, int readTimeOut, Object... parms) {
		return sendApiM(serverId, method, readTimeOut, parms);
	}
	
	protected static ResponseMessage apiMessage(int serverId,String method,int readTimeOut,Object...parms){
		return sendApiMessage(serverId, CrossController, method, readTimeOut, parms);
	}
	
	
	

	private static Object sendApiM(int serverId, String method, int readTimeOut, Object... parms) {
		ResponseMessage message = sendApiMessage(serverId, CrossController, method, readTimeOut, parms);
		return message == null ? null : message.m;
	}
	
	protected static Object sendApiMByContrl(int serverId,String contrl,String method, int readTimeOut, Object... parms) {
		ResponseMessage message = sendApiMessage(serverId, contrl, method, readTimeOut, parms);
		return message == null ? null : message.m;
	}
	
	
	
	private static ResponseMessage sendApiMessage(int serverId, String contrl, String method, int readTimeOut, Object... parms) {
		long a = System.currentTimeMillis();
		String cipher = getCipher(contrl, method);
		SocketRequest request = new SocketRequest(serverId, contrl, method, cipher, parms);
		if (readTimeOut > 0) {
			request.setReadTimeOut(readTimeOut);
		}
		ResponseMessage message = SocketClient.sendRequest(request);
		if (message == null) {
			LogUtil.getMain().debug(request.info() + " -> " + (System.currentTimeMillis() - a));
		}else if (ConstantsConfig.isLocalHost()){
			message.t = System.currentTimeMillis();
		}
		return message;
	}

	private static String getCipher(String... params) {
		StringBuilder param = new StringBuilder();
		for (String p : params) {
			param.append(p);
			param.append("\r\n");
		}
		param.append(TimeProcesser.getUnixTime());
		return Aes.encrypt(param.toString());
	}	
	
	
	protected static void areaMessage(final int area, final String method, final Object... parms) {
		Context.getTimerServer().execute(new Runnable() {

			@Override
			public void run() {
				Collection<CrossServer> servers = CrossServer.getAll();
				for (CrossServer e : servers) {
					if (area <= 0) {
						if (e.getType() <= 0) {
							continue;
						}
					} else if (e.getType() != area) {
						continue;
					}
					apiMessage(e.getServerId(), method, 10000, parms);
				}

			}
		});

	}	
}
